package main.java.middle;

import java.util.HashMap;

/**
 * @Author: 郑国荣
 * @Date: 2023-08-21-16:39
 * @Description:
 */
public class MajorityElement {
    public static int majorityElement(int[] nums){
        int len = nums.length;
        if (len==1) return nums[0];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : nums) {
            if (map.containsKey(num)) {
                int count = map.get(num)+1;
                if (count > len / 2) return num;
                map.replace(num, count);
            } else map.put(num, 1);
        }
        return 0;
    }




    public static void main(String[] args) {
        int[] ints = {2,2};
        System.out.println(majorityElement(ints));
    }

}
